package com.example.forum.service.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.dao.MessageMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Message;
import com.example.forum.model.User;
import com.example.forum.service.IMessageService;
import com.example.forum.service.IUserService;
import com.example.forum.utils.IdNotNullUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 功能描述:
 *
 * @author Lenovo
 * @date 2024/12/27
 */
@Slf4j
@Service
public class MessageServiceImpl implements IMessageService {
    @Resource
    private MessageMapper messageMapper;

    @Resource
    private IUserService userService;

    @Override
    public void create(Message message) {
        //非空校验
        if (message == null || message.getPostUserId() == null
                || message.getReceiveUserId() == null
                || !StringUtils.hasLength(message.getContent())) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //校验接收方
        User user = userService.selectById(message.getReceiveUserId());
        if (user == null) {
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //设置更新对象
        message.setState((byte) 0);
        message.setDeleteState((byte) 0);
        Date date = new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);
        //新增
        int row = messageMapper.insertSelective(message);
        if (row != 1) {
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
    }

    @Override
    public Message selectById(Long id) {
        IdNotNullUtil.idNotNullUtil(id);
        return messageMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId) {
        IdNotNullUtil.idNotNullUtil(receiveUserId);
        Integer count = messageMapper.selectUnreadCount(receiveUserId);
        if (count == null) {
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        return count;
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        IdNotNullUtil.idNotNullUtil(receiveUserId);
        return messageMapper.selectByReceiveUserId(receiveUserId);
    }

    @Override
    public void updateStateById(Long id, Byte state) {
        //参数校验
        IdNotNullUtil.idNotNullUtil(id);
        if (state < 0 || state > 2) {
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //Message message = messageMapper.selectByPrimaryKey(id);
        Message updateMessage = new Message();
        updateMessage.setId(id);
        updateMessage.setState(state);
        updateMessage.setUpdateTime(new Date());
        int row = messageMapper.updateByPrimaryKeySelective(updateMessage);
        if (row != 1) {
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public void reply(Long repliedId, Message message) {
        IdNotNullUtil.idNotNullUtil(repliedId);
        Message existsMessage = messageMapper.selectByPrimaryKey(repliedId);
        if (existsMessage == null || existsMessage.getDeleteState() == 1) {
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }
        //更新状态为已回复
        updateStateById(repliedId, (byte) 2);
        //创建回复信息
        create(message);
    }
}
